﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EIA.SLClient.Utility.Controls
{
    public class ContentControl4State : ContentControl
    {
        public ContentControl4State()
        {
            this.DefaultStyleKey = typeof(ContentControl4State);
            this.IsEnabledChanged += (s, e) =>
            {
                RepresendBackgroundAsync();
            };
        }

        protected void RepresentBackground()
        {
            if (!IsEnabled) this.Background = BackgroundDisabled;
            else
            {
                if (_is_left_mouse_down)
                {
                    this.Background = BackgroundPressed;
                }
                else
                {
                    if (_is_mouse_enter)
                    {
                        this.Background = BackgroundMouseOver;
                    }
                    else
                    {
                        this.Background = BackgroundNormal;
                    }
                }
            }
        }

        protected void RepresendBackgroundAsync()
        {
            this.Dispatcher.BeginInvoke(RepresentBackground);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.Background = BackgroundNormal;
        }
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            _is_mouse_enter = true;
            base.OnMouseEnter(e);
            RepresendBackgroundAsync();
        }
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            _is_mouse_enter = false;
            _is_left_mouse_down = false;
            base.OnMouseLeave(e);
            RepresendBackgroundAsync();
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            _is_left_mouse_down = true;
            base.OnMouseLeftButtonDown(e);
            RepresendBackgroundAsync();
        }
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            _is_left_mouse_down = false;
            base.OnMouseLeftButtonUp(e);
            RepresendBackgroundAsync();
        }
        bool _is_left_mouse_down = false;
        bool _is_mouse_enter = false;

        #region BackgroundNormal (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundNormal
        {
            get { return (Brush)GetValue(BackgroundNormalProperty); }
            set { SetValue(BackgroundNormalProperty, value); }
        }
        public static readonly DependencyProperty BackgroundNormalProperty =
            DependencyProperty.Register("BackgroundNormal", typeof(Brush), typeof(ContentControl4State),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundNormalChanged)));

        private static void OnBackgroundNormalChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            ContentControl4State source = (ContentControl4State)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundMouseOver (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundMouseOver
        {
            get { return (Brush)GetValue(BackgroundMouseOverProperty); }
            set { SetValue(BackgroundMouseOverProperty, value); }
        }
        public static readonly DependencyProperty BackgroundMouseOverProperty =
            DependencyProperty.Register("BackgroundMouseOver", typeof(Brush), typeof(ContentControl4State),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundMouseOverChanged)));

        private static void OnBackgroundMouseOverChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            ContentControl4State source = (ContentControl4State)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundPressed (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundPressed
        {
            get { return (Brush)GetValue(BackgroundPressedProperty); }
            set { SetValue(BackgroundPressedProperty, value); }
        }
        public static readonly DependencyProperty BackgroundPressedProperty =
            DependencyProperty.Register("BackgroundPressed", typeof(Brush), typeof(ContentControl4State),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundPressedChanged)));

        private static void OnBackgroundPressedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            ContentControl4State source = (ContentControl4State)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundDisabled (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundDisabled
        {
            get { return (Brush)GetValue(BackgroundDisabledProperty); }
            set { SetValue(BackgroundDisabledProperty, value); }
        }
        public static readonly DependencyProperty BackgroundDisabledProperty =
            DependencyProperty.Register("BackgroundDisabled", typeof(Brush), typeof(ContentControl4State),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundDisabledChanged)));

        private static void OnBackgroundDisabledChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            ContentControl4State source = (ContentControl4State)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

    }
}
